package com.leyou.search.web;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.entity.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {
    @Autowired
    SearchService searchService;

    /**
     * 初始化索引库
     * @return
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        searchService.createIndexAddMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    /**
     * 自动补全查询
     * @param key
     * @return
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String key) {
        return searchService.getSuggestion(key);
    }

    /**
     * 根据条件 分页，高亮查询商品
     * @param searchParamDTO
     * @return
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO searchParamDTO){
        return searchService.searchGoods(searchParamDTO);
    }

    /**
     * filter筛选条件，查询商品
     * @param searchParamDTO
     * @return
     */
    @PostMapping("filter")
    // TODO 为什么用 “Map<String,List<?>>” 返回数据
    public Mono<Map<String,List<?>>> listFilter(@RequestBody SearchParamDTO searchParamDTO) {
        return searchService.listFilter(searchParamDTO);
    }
}
